<?php
/*
*description:实时库存
*author:李飞@天众科技
*time:2017/05/04
*/
namespace Admin\Controller;
use Think\Controller;
use Think\Model;
class GoodsapiController extends Controller {
	//导入库存
	public function importArrivalorder(){
		$datas=file_get_contents('php://input');
		file_put_contents('./log/importArrivalorder.log',date('Y-m-d H:i:s').var_export($datas,true).PHP_EOL,FILE_APPEND);//日志导出sql
		$param=json_decode($datas,true);
		$warehouse_code	=$param['warehouse_code'];
		$this->db_config($warehouse_code);
		$ao=M('ArrivalOrder',C('DB_PRS'),C('DB_DEPOT'));
		$booksCate=M('BooksCate',C('DB_PRS'),C('DB_DEPOT'));
		foreach($param['rows'] as $k=>$v){
			$data=array();
			$data['order_no']		= $v['order_no'];
			$data['batch_no']		= $v['batch_no'];
			$data['manual_id']		= $v['manual_id'];
			$data['goods_no']		= $v['goods_no'];
			$data['goods_name']		= $v['goods_name'];
			$data['goods_spec']		= $v['goods_spec'];
			$data['unit1']		= $v['unit1'];
			$data['unit2']		= $v['unit2'];
			$data['storage_amount']		= $v['storage_amount'];
			$data['receive_amount']		= $v['receive_amount'];
			$data['price']		= $v['price'];
			$data['total_price']		= $v['total_price'];
			$data['country_code']		= $v['country_code'];
			$data['currency']		= $v['currency'];
			$data['bar_code']		= $v['bar_code'];
			$data['conforming_p']		= $v['conforming_p'];
			$data['nonconforming_p']		= $v['nonconforming_p'];
			$data['scrap_one']		= $v['scrap_one'];
			$data['overdue_time']		= $v['overdue_time'];
			$data['erp_code']		= $v['erp_code'];
			
			$data['status']			= 1;
			$data['inbound_status']	= 1;
			// $data['arrival_time']	= 1;
			// $data['old_goods_id']	= 1;
			$data['add_time']		= time();
			$data['update_time']	= time();
			$data['examine_time']	= time();
			
			$amount_s=$v['storage_amount'];
			$price_s= $v['price'];
			if(is_null($amount_s)){
				$return['status']=0;
				$return['message']='库存不能为空';
				$this->ajaxReturn($return);
			}
			if(is_null($price_s)){
				$return['status']=0;
				$return['message']='单价不能为空';
				$this->ajaxReturn($return);
			}
			if(is_null($data['erp_code'])){
				$return['status']=0;
				$return['message']='企业编码不能为空';
				$this->ajaxReturn($return);
			}
			$books_info=$booksCate->where(array('code'=>$v['manual_id']))->find();
			if(empty($books_info)){
				$return['status']=0;
				$return['message']='仓库中查不到此账册编码'.$v['manual_id'];
				$this->ajaxReturn($return);
			}
			//添加
			$res=$ao->add($data);
			// file_put_contents('./log/importArrivalorder.log',date('Y-m-d H:i:s').var_export($ao->getlastsql(),true).PHP_EOL,FILE_APPEND);//日志导出sql
			if(!$res){
				$return['status']=0;
				$return['message']='导入错误，请查看导入数据是否完整！';
				$this->ajaxReturn($return);
			}
		}
		$return['status']=1;
		$return['message']='导入成功！';
		$this->ajaxReturn($return);
	}
	//批次信息
	public function get_batchlist(){
		$datas=file_get_contents('php://input');
		file_put_contents('./log/get_batchlist.log',date('Y-m-d H:i:s').var_export($datas,true).PHP_EOL,FILE_APPEND);//日志导出sql
		$param=json_decode($datas,true);
		$warehouse_code	=$param['warehouse_code'];
		$this->db_config($warehouse_code);
		$ao=M('ArrivalOrder',C('DB_PRS'),C('DB_DEPOT'));
		$map['order_no']=$param['order_no'];
		$ao_list=$ao->field('batch_no,order_no,overdue_time,storage_amount')->where($map)->select();
		if(empty($ao_list)){
			$return['status']=0;
			$return['message']='没有此商品！';
			$this->ajaxReturn($return);
		}else{
			$return['status']=1;
			$return['message']='查询成功！';
			$return['data']=$ao_list;
			$this->ajaxReturn($return);
		}
	}
	//单个商品库存信息
	public function get_goodsinfo(){
		$datas=file_get_contents('php://input');
		file_put_contents('./log/get_goodsinfo.log',date('Y-m-d H:i:s').var_export($datas,true).PHP_EOL,FILE_APPEND);//日志导出sql
		$param=json_decode($datas,true);
		$warehouse_code	= $param['warehouse_code'];
		$goods_no=$param['goods_no'];
		
		// $goods_no='AO201706219310047208217';//测试商品编码
		// $warehouse_code='12345678';//测试仓库编码
		
		$this->db_config($warehouse_code);
		$ao=M('ArrivalOrder',C('DB_PRS'),C('DB_DEPOT'));
		$map=array();
		$map['status']=1;
		$map['inbound_status']=1;
		$map['goods_no']=$goods_no;
		$list=$ao->field("sum(storage_amount) storage_amount")->where($map)->group('goods_no')->select();
		// echo $ao->getlastsql();
		// die;
		if(empty($list)){
			$return['status']=0;
			$return['message']='没有查到对应数据';
			$return['data']='';
			$this->ajaxReturn($return);
		}else{
			$return['status']=1;
			$return['message']='查询成功！';
			$return['data']=$list;
			$this->ajaxReturn($return);
		}
	}
	//单个商品信息
	public function goods_info(){
		$datas=file_get_contents('php://input');
		file_put_contents('./log/goods_info.log',date('Y-m-d H:i:s').var_export($datas,true).PHP_EOL,FILE_APPEND);//日志导出sql
		$param=json_decode($datas,true);
		$warehouse_code	=$param['warehouse_code'];
		$id=$param['id'];
		$goods_no=$param['goods_no'];
		if(!empty($id)){
			$map['id']=$param['id'];
		}
		if(!empty($goods_no)){
			$map['goods_no']=$param['goods_no'];
		}
		$this->db_config($warehouse_code);
		$m=M('ArrivalOrder',C('DB_PRS'),C('DB_DEPOT'));
		$info=$m->where($map)->find();
		// $sql=$m->getlastsql();
		// file_put_contents('./log/goods_info.log',date('Y-m-d H:i:s').var_export($sql,true).PHP_EOL,FILE_APPEND);//日志导出sql
		if($info){
			$return['status']=1;
			$return['info']=$info;
			$this->ajaxReturn($return);
		}else{
			$return['status']=0;
			$return['info']='没有数据';
			$this->ajaxReturn($return);
		}
	}
	//库存商品列表-批次库存
	public function get_goodslist_batch(){
		$datas=file_get_contents('php://input');
		file_put_contents('./log/get_goodslist_batch.log',date('Y-m-d H:i:s').var_export($datas,true).PHP_EOL,FILE_APPEND);//日志导出sql
		// $datas='{"warehouse_code":"11330213","goods_no":"","goods_name":"","bar_code":"","manua_id":"AO201706211413","start_time":"2018-05-21","end_time":"2018-05-29","page":null,"rows":20,"sort":"id","order":"desc"}';
		// $datas='{"warehouse_code":"11330213","goods_no":null,"goods_name":null,"bar_code":null,"manua_id":"AO201706211413","start_time":null,"end_time":null,"page":null,"rows":20,"sort":"id","order":"desc"}';
		$param=json_decode($datas,true);
		$warehouse_code	=$param['warehouse_code'];
		$excel=$param['excel'];
		// $out_excel=$param['status'];
		
		if(empty($warehouse_code)){
			alert_json(0,"请传仓库编码");
		}
		$this->db_config($warehouse_code);
		$ao=M('ArrivalOrder',C('DB_PRS'),C('DB_DEPOT'));
        $m=M('Goods',C('DB_PRS'),C('DB_DEPOT'));
		$booksCate=M('BooksCate',C('DB_PRS'),C('DB_DEPOT'));
		$books=M('Books',C('DB_PRS'),C('DB_DEPOT'));
		
		$d=M('DistributionPeople');
        //搜索条件
        $map=array();
		$where=array();
        $goods_no=$param['goods_no'];
        $goods_name=$param['goods_name'];
		$bar_code=$param['bar_code'];
		$manua_id=$param['manua_id'];
		if(!empty($manua_id)){
			$map['m.manua_id']=array('in',$manua_id);
		}

		$start_time=$param['start_time'];//开始时间
		$end_time=$param['end_time'];//结束时间
		if(!empty($start_time) && empty($end_time)) {
			$start_time=strtotime($start_time);
			$where['_string'].=" a.overdue_time >= '$start_time' ";
        }
        if(!empty($end_time) && empty($start_time)) {
			$end_time=strtotime($end_time);
			$where['_string'].=" a.overdue_time <= '$end_time' ";
        }
		if(!empty($start_time) && !empty($end_time)) {
			$start_time=strtotime($start_time);
			$end_time=strtotime($end_time);
            // $where['a.overdue_time']= array(array('gt',I("start_time")),array('lt',I("end_time")));
			$where['_string'].=" (a.overdue_time >= '$start_time' and a.overdue_time <= '$end_time') ";
        }
		if(empty($start_time) && empty($end_time)) {
			$where['_string'].=" (a.overdue_time >= 0)";
        }
		
		$agent_id=$param['agent_id'];
		if(!empty($agent_id)){
			$d_info=$d->field('department_id,erp_code')->where(array('id'=>$agent_id))->find();//分销商角色id
			$erp_code=$d_info['erp_code'];
			$distribution_department_id=$d_info['department_id'];
			
			$u=M('User');
			$username=$u->where(array('id'=>$erp_code))->getField('username');
			$db_name=C('ERP_DB_PREFIX').$username;
			
			C('db_config_erp.DB_TYPE',C('DB_TYPE'));
			C('db_config_erp.DB_HOST',C('DB_HOST'));
			C('db_config_erp.DB_USER',C('DB_USER'));
			C('db_config_erp.DB_PWD',C('DB_PWD'));
			C('db_config_erp.DB_PORT',C('DB_PORT'));
			C('db_config_erp.DB_PREFIX',C('ERP_DB_PREFIX'));
			C('db_config_erp.DB_CHARSET',C('DB_CHARSET'));
			C('db_config_erp.DB_NAME',$db_name);
			
			$storage_config=M('StorageConfig',C('ERP_DB_PREFIX'),C('db_config_erp'));
			
		}

		$map['m.status']=1;
		$map['a.inbound_status']=1;
		$map['a.grounding_status']=1;
		// $map['a.plan_id']=2;
		
        !empty($goods_no)?$map['m.goods_no']=array('like','%'.$goods_no.'%'):'';
		!empty($goods_name)?$map['m.goods_name']=array('like','%'.$goods_name.'%'):'';
		!empty($bar_code)?$map['m.bar_code']=array('like','%'.$bar_code.'%'):'';
		
        $page=$param['page'];
        $rows=$param['rows'];
		
		if(empty($page)){
			$page=1;
		}
        //排序
        $sort=$param['sort'];
        $order=$param['order'];
		if(empty($sort)){
			$sort='id';
		}

		$info=$m->alias('m')->field('m.*,a.overdue_time,a.receive_amount,a.freez_amount,a.storage_amount,a.bad_product,a.conforming_p,a.nonconforming_p')->join("__ARRIVAL_ORDER__ a on m.goods_no=a.goods_no and m.manua_id=a.manual_id","right")->page($page,$rows)->where($map)->where($where)->order('m.'.$sort.' '.$order)->select();
		
		$info_list=$m->alias('m')->field('m.*,a.overdue_time,a.receive_amount,a.freez_amount,a.storage_amount')->join("__ARRIVAL_ORDER__ a on m.goods_no=a.goods_no and m.manua_id=a.manual_id","right")->where($map)->where($where)->select();
		$count=count($info_list);
		
		foreach($info as $k=>$v){
			$info[$k]['push_time']=date("Y-m-d H:i:s",$v['push_time']);

			if($v['grounding_status']==0){
				$info[$k]['grounding_status']='否';
			}
			if($v['grounding_status']==1){
				$info[$k]['grounding_status']='是';
			}
			
			// file_put_contents('./log/test.log',date('Y-m-d H:i:s').var_export($books->getlastsql(),true).PHP_EOL,FILE_APPEND);//日志导出sql
			
			if(!empty($agent_id)){
				$info[$k]['sell_price']=$storage_config->where(array('goods_no'=>$v['goods_no'],'distribution_department_id'=>$distribution_department_id))->getField('price_config');
				$storage_alert_amount_config=$storage_config->where(array('goods_no'=>$v['goods_no'],'distribution_department_id'=>$distribution_department_id))->getField('storage_alert_amount_config');
				$info[$k]['storage_amount']=$v['storage_amount']-$storage_alert_amount_config;
				// file_put_contents('./log/test.log',date('Y-m-d H:i:s').var_export($storage_config->getlastsql(),true).PHP_EOL,FILE_APPEND);//日志导出sql
				
			}
			$unit1_no=$books->where(array('goods_no'=>$v['goods_no']))->getField('unit1');
			$unit2_no=$books->where(array('goods_no'=>$v['goods_no']))->getField('unit2');
			$info[$k]['unit1']=M('Unit')->where(array('unit_no'=>$unit1_no))->getField('unit_name');
			$info[$k]['unit2']=M('Unit')->where(array('unit_no'=>$unit2_no))->getField('unit_name');
			$info[$k]['current_type']=M('Coin')->where(array('coin_code'=>$v['current_type']))->getField('coin_name');
		}
		
		if(!empty($excel)){ //导出Excel
			$xlsName  = "商品库存列表";
			$xlsCell  = array(
				array('goods_no','商品编码'),
				array('manua_id','账册编号'),
				array('goods_name','商品名称'),
				array('goods_cate_name','商品类型'),
				array('bar_code','条形码'),
				array('goods_spec','规格'),
				array('unit1','第一单位'),
				array('unit2','第二单位'),
				array('sell_price','销售价格'),
				array('amount','总数量'),
				array('storage_amount','库存数量'),
				array('freez_amount','冻结数量'),
				array('bad_product','不良品'),
				array('scrap_one','报废品'),
				array('push_times','推送次数'),
				array('info_count','批次数量')
			);
			$this->exportExcel($xlsName,$xlsCell,$info);
			die;
		}
		
        if(!empty($info)){
            $data['total']=$count;
            $data['rows']=$info;
        }else{
            $data['total']=0;
            $data['rows']=0;
        }
		
        $this->ajaxReturn($data);
    }
    public function get_goodslist_batch_erp(){
        $datas=file_get_contents('php://input');
        file_put_contents('./log/get_goodslist_batch_erp.log',date('Y-m-d H:i:s').var_export($datas,true).PHP_EOL,FILE_APPEND);//日志导出sql
        // $datas='{"warehouse_code":"11330213","goods_no":"","goods_name":"","bar_code":"","manua_id":"AO201706211413","start_time":"2018-05-21","end_time":"2018-05-29","page":null,"rows":20,"sort":"id","order":"desc"}';
        // $datas='{"warehouse_code":"11330213","goods_no":null,"goods_name":null,"bar_code":null,"manua_id":"AO201706211413","start_time":null,"end_time":null,"page":null,"rows":20,"sort":"id","order":"desc"}';
        $param=json_decode($datas,true);
        $warehouse_code	=$param['warehouse_code'];
        // $out_excel=$param['status'];

        if(empty($warehouse_code)){
            alert_json(0,"请传仓库编码");
        }
        $this->db_config($warehouse_code);
        $ao=M('ArrivalOrder',C('DB_PRS'),C('DB_DEPOT'));
        $m=M('Goods',C('DB_PRS'),C('DB_DEPOT'));
        $booksCate=M('BooksCate',C('DB_PRS'),C('DB_DEPOT'));
        $books=M('Books',C('DB_PRS'),C('DB_DEPOT'));

        $d=M('DistributionPeople');
        //搜索条件
        $map=array();
        $where=array();
        $goods_no=$param['goods_no'];
        $goods_name=$param['goods_name'];
        $bar_code=$param['bar_code'];
        $manua_id=$param['manua_id'];
        if(!empty($manua_id)){
            $map['m.manua_id']=array('in',$manua_id);
        }

        $start_time=$param['start_time'];//开始时间
        $end_time=$param['end_time'];//结束时间
        if(!empty($start_time) && empty($end_time)) {
            $start_time=strtotime($start_time);
            $where['_string'].=" a.overdue_time >= '$start_time' ";
        }
        if(!empty($end_time) && empty($start_time)) {
            $end_time=strtotime($end_time);
            $where['_string'].=" a.overdue_time <= '$end_time' ";
        }
        if(!empty($start_time) && !empty($end_time)) {
            $start_time=strtotime($start_time);
            $end_time=strtotime($end_time);
            // $where['a.overdue_time']= array(array('gt',I("start_time")),array('lt',I("end_time")));
            $where['_string'].=" (a.overdue_time >= '$start_time' and a.overdue_time <= '$end_time') ";
        }
        if(empty($start_time) && empty($end_time)) {
            $where['_string'].=" (a.overdue_time >= 0)";
        }

        $agent_id=$param['agent_id'];
        if(!empty($agent_id)){
            $d_info=$d->field('department_id,erp_code')->where(array('id'=>$agent_id))->find();//分销商角色id
            $erp_code=$d_info['erp_code'];
            $distribution_department_id=$d_info['department_id'];

            $u=M('User');
            $username=$u->where(array('id'=>$erp_code))->getField('username');
            $db_name=C('ERP_DB_PREFIX').$username;

            C('db_config_erp.DB_TYPE',C('DB_TYPE'));
            C('db_config_erp.DB_HOST',C('DB_HOST'));
            C('db_config_erp.DB_USER',C('DB_USER'));
            C('db_config_erp.DB_PWD',C('DB_PWD'));
            C('db_config_erp.DB_PORT',C('DB_PORT'));
            C('db_config_erp.DB_PREFIX',C('ERP_DB_PREFIX'));
            C('db_config_erp.DB_CHARSET',C('DB_CHARSET'));
            C('db_config_erp.DB_NAME',$db_name);

            $storage_config=M('StorageConfig',C('ERP_DB_PREFIX'),C('db_config_erp'));

        }

        $map['m.status']=1;
        $map['a.inbound_status']=1;
        $map['a.grounding_status']=1;
        $map['a.storage_amount'] = array('GT',0);
        // $map['a.plan_id']=2;

        !empty($goods_no)?$map['m.goods_no']=array('like','%'.$goods_no.'%'):'';
        !empty($goods_name)?$map['m.goods_name']=array('like','%'.$goods_name.'%'):'';
        !empty($bar_code)?$map['m.bar_code']=array('like','%'.$bar_code.'%'):'';

        $page=$param['page'];
        $rows=$param['rows'];

        if(empty($page)){
            $page=1;
        }
        //排序
        $sort=$param['sort'];
        $order=$param['order'];
        if(empty($sort)){
            $sort='id';
        }

        $info=$m->alias('m')->field('m.*,a.overdue_time,a.receive_amount,a.freez_amount,a.storage_amount,a.bad_product,a.conforming_p,a.nonconforming_p')->join("__ARRIVAL_ORDER__ a on m.goods_no=a.goods_no and m.manua_id=a.manual_id","right")->page($page,$rows)->where($map)->where($where)->order('m.'.$sort.' '.$order)->select();

        $info_list=$m->alias('m')->field('m.*,a.overdue_time,a.receive_amount,a.freez_amount,a.storage_amount')->join("__ARRIVAL_ORDER__ a on m.goods_no=a.goods_no and m.manua_id=a.manual_id","right")->where($map)->where($where)->select();
        $count=count($info_list);

        foreach($info as $k=>$v){
            $info[$k]['push_time']=date("Y-m-d H:i:s",$v['push_time']);
            $info[$k]['bad_product']+=$v['nonconforming_p'];
            if($v['grounding_status']==0){
                $info[$k]['grounding_status']='否';
            }
            if($v['grounding_status']==1){
                $info[$k]['grounding_status']='是';
            }

            // file_put_contents('./log/test.log',date('Y-m-d H:i:s').var_export($books->getlastsql(),true).PHP_EOL,FILE_APPEND);//日志导出sql

            if(!empty($agent_id)){
                $info[$k]['sell_price']=$storage_config->where(array('goods_no'=>$v['goods_no'],'distribution_department_id'=>$distribution_department_id))->getField('price_config');
                $storage_alert_amount_config=$storage_config->where(array('goods_no'=>$v['goods_no'],'distribution_department_id'=>$distribution_department_id))->getField('storage_alert_amount_config');
                $info[$k]['storage_amount']=$v['storage_amount']-$storage_alert_amount_config;
                // file_put_contents('./log/test.log',date('Y-m-d H:i:s').var_export($storage_config->getlastsql(),true).PHP_EOL,FILE_APPEND);//日志导出sql

            }
            $unit1_no=$books->where(array('goods_no'=>$v['goods_no']))->getField('unit1');
            $unit2_no=$books->where(array('goods_no'=>$v['goods_no']))->getField('unit2');
            $info[$k]['unit1']=M('Unit')->where(array('unit_no'=>$unit1_no))->getField('unit_name');
            $info[$k]['unit2']=M('Unit')->where(array('unit_no'=>$unit2_no))->getField('unit_name');
            $info[$k]['current_type']=M('Coin')->where(array('coin_code'=>$v['current_type']))->getField('coin_name');
        }

        if(!empty($info)){
            $data['total']=$count;
            $data['rows']=$info;
        }else{
            $data['total']=0;
            $data['rows']=0;
        }

        $this->ajaxReturn($data);
    }

    //库存商品列表-商家自编码、批次号
    public function get_goodslsit_batch_custom(){
        $datas=file_get_contents('php://input');
        $datas=urldecode(trim($datas,'='));

        if(empty($datas)){
            $this->ajaxReturn(['status'=>0,'message'=>'参数为空']);
        }
        file_put_contents('./log/get_goodslist_batch_custom.log',date('Y-m-d H:i:s').' '.var_export($datas,true).PHP_EOL,FILE_APPEND);//日志导出sql
        //$datas='{"data":{"customer":{"warehouse_code":"11330213","user_code":"CS12345678"},"goods_sn":["AO201706219343925000078","T1234567T9343925000078","CS123456789343925000078"]}}';
        $param=json_decode($datas,true);
        $data=$param['data'];
        //exit(var_dump($data));

        $warehouse_code	= $data['customer']['warehouse_code'];
        $goods_no=$data['goods_sn'];

        $this->db_config($warehouse_code);
        $ao=M('ArrivalOrder',C('DB_PRS'),C('DB_DEPOT'));
        $map=array();
        $map['status']=1;
        $map['inbound_status']=1;
        $map['goods_no']=array('in',$goods_no);

        $booksCate=M('BooksCate',C('DB_PRS'),C('DB_DEPOT'));
        $merchant_code=$data['customer']['user_code'];
        if(!empty($merchant_code)){
            $merchant_code=explode(",",$merchant_code);
            $b_info=$booksCate->field('code')->where(['merchant_code'=>['in',$merchant_code]])->select();

            $manua_id=[];
            foreach($b_info as $k=>$v){
                $manua_id[]=$v['code'];
            }
            $map['manual_id']=array('in',$manua_id);
        }
        //echo json_encode($map);exit;
        $list=$ao->field("sum(storage_amount) storage_amount,sum(bad_product) bad_product,sum(nonconforming_p) nonconforming_p,goods_no,goods_sn_custom,batch")->where($map)->group('goods_no')->select();

        // echo $ao->getlastsql();
        // die;
        if(empty($list)){
            $return['status']=0;
            $return['message']='没有查到对应数据';
            $return['data']='';
            $this->ajaxReturn($return);
        }else{
            $return['status']=1;
            $return['message']='查询成功！';
            $return['data']=$list;
            $this->ajaxReturn($return);
        }
    }
    //出入库记录查询
    public function stock_check(){
        $datas=file_get_contents('php://input');
        file_put_contents('./log/get_order_log_erp.log',date('Y-m-d H:i:s').var_export($datas,true).PHP_EOL,FILE_APPEND);//日志导出sql
        $param=json_decode($datas,true);
        //$param['action']='In';
        //$param['manual_id']='YZ201710130001';
        //$param['company_code']='YZ20170921';
        $customer_config = array(
            'YZ20170921'=>'永卓',
        );
        $data = array(
            'warehouse'=>$param['warehouse']?$param['warehouse']:'11330213',//默认为11330213 天众下沙仓
            'manual_id'=>$param['manual_id'],//账册编号不能为空
            'company_name'=>$customer_config[$param['company_code']],//不能为空
            'size'=>$param['size']?$param['size']:20,//默认为20
            'page'=>$param['page']?$param['page']:1,//默认为1
            'action'=>$param['action'],//默认为空
            'order_no'=>$param['order_no'],
        );
        if(!$param['action']){
            echo json_encode(array('code'=>0,'data'=>'未设置出入库参数'));
            exit;
        }
        elseif($param['action']=='In'){
           $this->get_order_in_log($data);
        }elseif($param['action']=='Out'){
           $this->get_order_out_log($data);
        }
        exit('?');
    }
    //获取入库记录
    public function get_order_in_log($param){
        if($param['warehouse']!='11330213'){//todo:new warehouse
            echo json_encode(array('code'=>0,'data'=>'未设置仓库参数'));
            exit;
        }
        if(!trim($param['manual_id'])){
            echo json_encode(array('code'=>0,'data'=>'未设置商家账册参数'));
            exit;
        }

        $this->db_config('11330213');//天众下沙仓
        $m=M('ArrivalOrderPlan',C('DB_PRS'),C('DB_DEPOT'));
        $ck=A('CheckInput');

        //搜索条件
        $map=array();
        $map['manual_id']=$param['manual_id'];
        if(!empty($param['order_no'])){
            $map['order_no']=array('in',$param['order_no']);
        }
        $map['status']=1;
        // $map['times']=array('neq',0);
        $page=$param['page'];
        $rows=$param['size'];

        //排序
        $sort=$ck->in('排序','sort','cnennumstr','examine_time',0,0);
        $order=$ck->in('规则','order','cnennumstr','desc',0,0);

        $count=$m->where($map)->count();//指定商家、已出库、is_emergency=0，按出库时间delivery_time倒序
        if(empty($map)){
            $info=$m->field('id,id ids,batch_no,order_no,manual_id,customs_code,gross_weight,net_weight,amount,wrap_type,add_time,examine_time,old_order_no,inbound_status')->page($page,$rows)->order($sort.' '.$order)->select();
        }else{
            $info=$m->field("id,id ids,batch_no,order_no,manual_id,customs_code,gross_weight,net_weight,amount,wrap_type,add_time,examine_time,old_order_no,inbound_status")->where($map)->page($page,$rows)->order($sort.' '.$order)->select();
        }

        foreach($info as $k=>$v){
            $info[$k]['add_time']= date('Y-m-d H:i:s',$v['add_time']);
            $info[$k]['examine_time']= date('Y-m-d H:i:s',$v['examine_time']);
            /*if(!empty($v['push_outboundtime'])){
                $info[$k]['push_outboundtime']=date('Y-m-d H:i:s',$v['push_outboundtime']);
            }else{
                $info[$k]['push_outboundtime']="";
            }
            if(!empty($v['delivery_time'])){
                $info[$k]['delivery_time']=date('Y-m-d H:i:s',$v['delivery_time']);
            }else{
                $info[$k]['delivery_time']="";
            }*/
        }
        // echo $m->getlastsql();
        // dump($info);
        // die;
        if(!empty($info)){
            echo json_encode(array('code'=>1,'count'=>$count,'data'=>$info));
            exit;
        }else{
            echo json_encode(array('code'=>0,'data'=>'没有记录'));
            exit;
        }
        exit('?');
    }
    //获取出库记录
    public function get_order_out_log($param){
        if($param['warehouse']!='11330213'){
            echo json_encode(array('code'=>0,'data'=>'未设置仓库参数'));
            exit;
        }
        if(!trim($param['company_name'])){
            echo json_encode(array('code'=>0,'data'=>'未设置商家参数'));
            exit;
        }

        $this->db_config('11330213');//天众下沙仓
        $m=M('Pushorder',C('DB_PRS'),C('DB_DEPOT'));
        $ck=A('CheckInput');

        //搜索条件
        $map=array();
        $map['company_name']=$param['company_name'];
        if(!empty($param['order_no'])){
            $map['order_no']=array('in',$param['order_no']);
        }
        // $map['times']=array('neq',0);
        $map['is_delivery']=1;

        $is_emergency=I('is_emergency','',trim);
        if(empty($is_emergency)){
            $map['is_emergency']=0;
        }
        if($is_emergency==1){
            $map['is_emergency']=1;
        }

        $start_time=I('start_time','',trim);
        $end_time=I('end_time','',trim);
        $time_cate=I('time_cate','',trim);
        $has_push=I('has_push');

        if($has_push==1){
            $map['push_outboundstatus']=1;
        }
        if($has_push==2){
            $map['push_outboundstatus']=0;
        }

        $page=$param['page'];
        $rows=$param['size'];
        //排序
        $sort=$ck->in('排序','sort','cnennumstr','delivery_time',0,0);
        $order=$ck->in('规则','order','cnennumstr','desc',0,0);

        $count=$m->where($map)->count();//指定商家、已出库、is_emergency=0，按出库时间delivery_time倒序
        if(empty($map)){
            $info=$m->field('id,id ids,order_no,user_name,user_phone,user_idcard,user_address,is_delivery,logistics_no,logiscompany_name,old_order_no,delivery_time,push_outboundstatus,push_outboundtime,push_outboundmark')->page($page,$rows)->order($sort.' '.$order)->select();
        }else{
            $info=$m->field('id,id ids,order_no,user_name,user_phone,user_idcard,user_address,is_delivery,logistics_no,logiscompany_name,old_order_no,delivery_time,push_outboundstatus,push_outboundtime,push_outboundmark')->where($map)->page($page,$rows)->order($sort.' '.$order)->select();
        }

        foreach($info as $k=>$v){
            //pushordergoods
            // if(!empty($excel)){
            // echo $m->getlastsql();
            // die;
            // }
            if(!empty($v['push_outboundtime'])){
                $info[$k]['push_outboundtime']=date('Y-m-d H:i:s',$v['push_outboundtime']);
            }else{
                $info[$k]['push_outboundtime']="";
            }
            if(!empty($v['delivery_time'])){
                $info[$k]['delivery_time']=date('Y-m-d H:i:s',$v['delivery_time']);
            }else{
                $info[$k]['delivery_time']="";
            }
        }
        if(!empty($info)){
            echo json_encode(array('code'=>1,'count'=>$count,'data'=>$info));
            exit;
        }else{
            echo json_encode(array('code'=>0,'data'=>'没有记录'));
            exit;
        }
       exit('?');
    }
	//库存商品列表-带销售价-永卓更新
	public function get_goodslist(){
		$datas=file_get_contents('php://input');
		file_put_contents('./log/get_goodslist.log',date('Y-m-d H:i:s').var_export($datas,true).PHP_EOL,FILE_APPEND);//日志导出sql
		$param=json_decode($datas,true);
		$warehouse_code	=$param['warehouse_code'];
		$excel=$param['excel'];
		$out_excel=$param['status'];
		$this->db_config($warehouse_code);
		$ao=M('ArrivalOrder',C('DB_PRS'),C('DB_DEPOT'));
        $m=M('Goods',C('DB_PRS'),C('DB_DEPOT'));
		$booksCate=M('BooksCate',C('DB_PRS'),C('DB_DEPOT'));
		$books=M('Books',C('DB_PRS'),C('DB_DEPOT'));
		
		$d=M('DistributionPeople');
        //搜索条件
        $map=array();
        $goods_no=$param['goods_no'];
        $goods_name=$param['goods_name'];
		$bar_code=$param['bar_code'];
		$manua_id=$param['manua_id'];
		if(!empty($manua_id)){
			$map['m.manua_id']=array('in',$manua_id);
		}
		$merchant_code=$param['merchant_code'];
		if(!empty($merchant_code)){
			$b_info=$booksCate->field('code')->where(['merchant_code'=>['in',"$merchant_code"]])->select();
			$manua_id=[];
			foreach($b_info as $k=>$v){
				$manua_id[]=$v['code'];
			}
			$map['m.manua_id']=array('in',$manua_id);
		}
		$agent_id=$param['agent_id'];
		if(!empty($agent_id)){
			$d_info=$d->field('department_id,erp_code')->where(array('id'=>$agent_id))->find();//分销商角色id
			$erp_code=$d_info['erp_code'];
			$distribution_department_id=$d_info['department_id'];
			
			$u=M('User');
			$username=$u->where(array('id'=>$erp_code))->getField('username');
			$db_name=C('ERP_DB_PREFIX').$username;
			
			C('db_config_erp.DB_TYPE',C('DB_TYPE'));
			C('db_config_erp.DB_HOST',C('DB_HOST'));
			C('db_config_erp.DB_USER',C('DB_USER'));
			C('db_config_erp.DB_PWD',C('DB_PWD'));
			C('db_config_erp.DB_PORT',C('DB_PORT'));
			C('db_config_erp.DB_PREFIX',C('ERP_DB_PREFIX'));
			C('db_config_erp.DB_CHARSET',C('DB_CHARSET'));
			C('db_config_erp.DB_NAME',$db_name);
			
			$storage_config=M('StorageConfig',C('ERP_DB_PREFIX'),C('db_config_erp'));
			
		}

		$map['m.status']=1;
		$map['a.inbound_status']=1;
		$map['a.grounding_status']=1;
		// $map['a.plan_id']=2;
		
        !empty($goods_no)?$map['m.goods_no']=array('like','%'.$goods_no.'%'):'';
		!empty($goods_name)?$map['m.goods_name']=array('like','%'.$goods_name.'%'):'';
		!empty($bar_code)?$map['m.bar_code']=array('like','%'.$bar_code.'%'):'';
		
		$goods_cate=I('goods_cate');
		if(!empty($goods_cate)){
			if($goods_cate==1000){
				$map['m.goods_cate']=array('neq',1);
			}else{
				$map['m.goods_cate']=$goods_cate;
			}	
		}
        $page=$param['page'];
        $rows=$param['rows'];

        //排序
        $sort=$param['sort'];
		if(empty($sort)){
			$sort='id';
		}		   
        $order=$param['order'];

		$info=$m->alias('m')->field('m.*,count(a.freez_amount) info_count,sum(a.freez_amount) freez_amount,sum(a.storage_amount) storage_amount,sum(a.bad_product) bad_product,sum(a.conforming_p) conforming_p,sum(a.nonconforming_p) nonconforming_p')->join("__ARRIVAL_ORDER__ a on m.goods_no=a.goods_no and m.manua_id=a.manual_id","left")->group('m.goods_no,m.manua_id')->page($page,$rows)->where($map)->order('m.'.$sort.' '.$order)->select();
		file_put_contents('./log/test.log',date('Y-m-d H:i:s').var_export($m->getlastsql(),true).PHP_EOL,FILE_APPEND);//日志导出sql
		// file_put_contents('./log/test.log',date('Y-m-d H:i:s').var_export(C('DB_PRS'),true).PHP_EOL,FILE_APPEND);//日志导出sql
		
		$info_list=$m->alias('m')->field('m.*,count(a.freez_amount) info_count,sum(a.freez_amount) freez_amount,sum(a.storage_amount) storage_amount')->join("__ARRIVAL_ORDER__ a on m.goods_no=a.goods_no and m.manua_id=a.manual_id","left")->group('m.goods_no,m.manua_id')->where($map)->select();
		$count=count($info_list);
		
		foreach($info as $k=>$v){
			$info[$k]['push_time']=date("Y-m-d H:i:s",$v['push_time']);

			if($v['grounding_status']==0){
				$info[$k]['grounding_status']='否';
			}
			if($v['grounding_status']==1){
				$info[$k]['grounding_status']='是';
			}
			if($v['goods_cate']==1){
				$info[$k]['goods_cate_name']="一般商品";
			}
			if($v['goods_cate']==2){
				$info[$k]['goods_cate_name']="原料";
			}
			if($v['goods_cate']==3){
				$info[$k]['goods_cate_name']="辅料";
			}
			
			// file_put_contents('./log/test.log',date('Y-m-d H:i:s').var_export($books->getlastsql(),true).PHP_EOL,FILE_APPEND);//日志导出sql
			
			if(!empty($agent_id)){
				$info[$k]['sell_price']=$storage_config->where(array('goods_no'=>$v['goods_no'],'distribution_department_id'=>$distribution_department_id))->getField('price_config');
				$storage_alert_amount_config=$storage_config->where(array('goods_no'=>$v['goods_no'],'distribution_department_id'=>$distribution_department_id))->getField('storage_alert_amount_config');
				$info[$k]['storage_amount']=$v['storage_amount']-$storage_alert_amount_config;
				// file_put_contents('./log/test.log',date('Y-m-d H:i:s').var_export($storage_config->getlastsql(),true).PHP_EOL,FILE_APPEND);//日志导出sql
				
			}
			$unit1_no=$books->where(array('goods_no'=>$v['goods_no']))->getField('unit1');
			$unit2_no=$books->where(array('goods_no'=>$v['goods_no']))->getField('unit2');
			$info[$k]['unit1']=M('Unit')->where(array('unit_no'=>$unit1_no))->getField('unit_name');
			$info[$k]['unit2']=M('Unit')->where(array('unit_no'=>$unit2_no))->getField('unit_name');
			$info[$k]['current_type']=M('Coin')->where(array('coin_code'=>$v['current_type']))->getField('coin_name');
			$info[$k]['merchant_code']=$booksCate->where(array('code'=>$v['manua_id']))->getField('merchant_code');
		}
		
		if(!empty($excel)){ //导出Excel
			$xlsName  = "商品库存列表";
			$xlsCell  = array(
				array('goods_no','商品编码'),
				array('manua_id','账册编号'),
				array('goods_name','商品名称'),
				array('goods_cate_name','商品类型'),
				array('bar_code','条形码'),
				array('goods_spec','规格'),
				array('unit1','第一单位'),
				array('unit2','第二单位'),
				array('sell_price','销售价格'),
				array('amount','总数量'),
				array('storage_amount','库存数量'),
				array('freez_amount','冻结数量'),
				array('bad_product','不良品'),
				array('scrap_one','报废品'),
				array('push_times','推送次数'),
				array('info_count','批次数量')
			);
			$this->exportExcel($xlsName,$xlsCell,$info);
			die;
		}
		
        if(!empty($info)){
            $data['total']=$count;
            $data['rows']=$info;
        }else{
            $data['total']=0;
            $data['rows']=0;
        }
		
        $this->ajaxReturn($data);
    }
	//多个商品库存信息
	public function get_goodsinfomany(){
		$datas=I();
		file_put_contents('./log/get_goodsinfomany.log',date('Y-m-d H:i:s').var_export($datas,true).PHP_EOL,FILE_APPEND);//日志导出sql
		// $datas=array (
		  // 'data' => '{"identity":{"sign":"45509fe6a578952e0d1654760fd8f3ed","timestamp":1542342492},"data":{"customer":{"warehouse_code":"11330213","user_code":"CS12345678"},"goods_sn":["AO201706219343925000078","AO201706219343925000078","T1234567T9343925000078","CS123456789343925000078"]}}',
		// );
		$param=json_decode(htmlspecialchars_decode($datas['data']),true);
		$data=$param['data'];
		$warehouse_code	= $data['customer']['warehouse_code'];
		$goods_no=$data['goods_sn'];
		

		$this->db_config($warehouse_code);
		$ao=M('ArrivalOrder',C('DB_PRS'),C('DB_DEPOT'));
		$map=array();
		$map['status']=1;
		$map['inbound_status']=1;
		$map['goods_no']=array('in',$goods_no);
		
		$booksCate=M('BooksCate',C('DB_PRS'),C('DB_DEPOT'));
		$merchant_code=$data['customer']['user_code'];
		if(!empty($merchant_code)){
			$merchant_code=explode(",",$merchant_code);
			$b_info=$booksCate->field('code')->where(['merchant_code'=>['in',$merchant_code]])->select();
			
			$manua_id=[];
			foreach($b_info as $k=>$v){
				$manua_id[]=$v['code'];
			}
			$map['manual_id']=array('in',$manua_id);
		}
		
		$list=$ao->field("sum(storage_amount) storage_amount,sum(bad_product) bad_product,sum(nonconforming_p) nonconforming_p,goods_no")->where($map)->group('goods_no')->select();

		// echo $ao->getlastsql();
		// die;
		if(empty($list)){
			$return['status']=0;
			$return['message']='没有查到对应数据';
			$return['data']='';
			$this->ajaxReturn($return);
		}else{
			$return['status']=1;
			$return['message']='查询成功！';
			$return['data']=$list;
			$this->ajaxReturn($return);
		}
	}
	//库存商品列表
	public function get_goodslist_bak(){
		$datas=file_get_contents('php://input');
		file_put_contents('./log/get_goodslist.log',date('Y-m-d H:i:s').var_export($datas,true).PHP_EOL,FILE_APPEND);//日志导出sql
		$param=json_decode($datas,true);
		$warehouse_code	=$param['warehouse_code'];
		$excel=$param['excel'];
		$out_excel=$param['status'];
		$this->db_config($warehouse_code);
		$ao=M('ArrivalOrder',C('DB_PRS'),C('DB_DEPOT'));
        $m=M('Goods',C('DB_PRS'),C('DB_DEPOT'));
		$booksCate=M('BooksCate',C('DB_PRS'),C('DB_DEPOT'));
		
        //搜索条件
        $map=array();
        $goods_no=$param['goods_no'];
        $goods_name=$param['goods_name'];
		$bar_code=$param['bar_code'];
		$manua_id=$param['manua_id'];
		$map['m.manua_id']=array('in',$manua_id);

		$map['m.status']=1;
		// $map['a.plan_id']=2;
		
        !empty($goods_no)?$map['m.goods_no']=array('like','%'.$goods_no.'%'):'';
		!empty($goods_name)?$map['m.goods_name']=array('like','%'.$goods_name.'%'):'';
		!empty($bar_code)?$map['m.bar_code']=array('like','%'.$bar_code.'%'):'';
		
		$goods_cate=I('goods_cate');
		if(!empty($goods_cate)){
			if($goods_cate==1000){
				$map['m.goods_cate']=array('neq',1);
			}else{
				$map['m.goods_cate']=$goods_cate;
			}	
		}
        $page=$param['page'];
        $rows=$param['rows'];

        //排序
        $sort=$param['sort'];
        $order=$param['order'];

		$info=$m->alias('m')->field('m.*,count(a.freez_amount) info_count,sum(a.freez_amount) freez_amount,sum(a.storage_amount) storage_amount')->join("__ARRIVAL_ORDER__ a on m.goods_no=a.goods_no and m.manua_id=a.manual_id","left")->group('m.goods_no,m.manua_id')->page($page,$rows)->where($map)->order('m.'.$sort.' '.$order)->select();
		// echo $m->getlastsql();
		file_put_contents('./log/get_goodslist.log',date('Y-m-d H:i:s').var_export($m->getlastsql(),true).PHP_EOL,FILE_APPEND);//日志导出sql
		$info_list=$m->alias('m')->field('m.*,count(a.freez_amount) info_count,sum(a.freez_amount) freez_amount,sum(a.storage_amount) storage_amount')->join("__ARRIVAL_ORDER__ a on m.goods_no=a.goods_no and m.manua_id=a.manual_id","left")->group('m.goods_no,m.manua_id')->where($map)->select();
		$count=count($info_list);
		foreach($info as $k=>$v){
			$info[$k]['push_time']=date("Y-m-d H:i:s",$v['push_time']);

			if($v['grounding_status']==0){
				$info[$k]['grounding_status']='否';
			}
			if($v['grounding_status']==1){
				$info[$k]['grounding_status']='是';
			}
			if($v['goods_cate']==1){
				$info[$k]['goods_cate_name']="一般商品";
			}
			if($v['goods_cate']==2){
				$info[$k]['goods_cate_name']="原料";
			}
			if($v['goods_cate']==3){
				$info[$k]['goods_cate_name']="辅料";
			}
			$info[$k]['unit1']=M('Unit')->where(array('unit_no'=>$v['unit1']))->getField('unit_name');
			$info[$k]['unit2']=M('Unit')->where(array('unit_no'=>$v['unit2']))->getField('unit_name');
			$info[$k]['current_type']=M('Coin')->where(array('coin_code'=>$v['current_type']))->getField('coin_name');
		}
		
		if(!empty($excel)){ //导出Excel
			$xlsName  = "商品库存列表";
			$xlsCell  = array(
				array('goods_no','商品编码'),
				array('manua_id','账册编号'),
				array('goods_name','商品名称'),
				array('goods_cate_name','商品类型'),
				array('bar_code','条形码'),
				array('goods_spec','规格'),
				array('unit1','第一单位'),
				array('unit2','第二单位'),
				array('goods_price','入库平均价格'),
				array('amount','总数量'),
				array('storage_amount','库存数量'),
				array('freez_amount','冻结数量'),
				array('nonconforming_p','残次品'),
				array('scrap_one','报废品'),
				array('push_times','推送次数'),
				array('info_count','批次数量')
			);
			$this->exportExcel($xlsName,$xlsCell,$info);
			die;
		}
		
        if(!empty($info)){
            $data['total']=$count;
            $data['rows']=$info;
        }else{
            $data['total']=0;
            $data['rows']=0;
        }
		
        $this->ajaxReturn($data);
    }
	//excel导出方法
	public function exportExcel($expTitle,$expCellName,$expTableData){
			$xlsTitle = iconv('utf-8', 'gb2312', $expTitle);//文件名称
			$fileName = $xlsTitle.date('_YmdHis');//or $xlsTitle 文件名称可根据自己情况设定
			$cellNum = count($expCellName);
			$dataNum = count($expTableData);
			//vendor("PHPExcel.PHPExcel");
		   
			//导入PHPExcel类库，因为PHPExcel没有用命名空间，只能inport导入
			import("Org.Util.PHPExcel");
			import("Org.Util.PHPExcel.Writer.Excel5");
			import("Org.Util.PHPExcel.IOFactory.php");
		   
		    $date = date("Y_m_d",time());
			$fileName .= "_{$date}.xls";

			//创建PHPExcel对象，注意，不能少了\
			$objPHPExcel = new \PHPExcel();
			//$objProps = $objPHPExcel->getProperties();
			
			//$objPHPExcel = new PHPExcel();
			$cellName = array('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','AA','AB','AC','AD','AE','AF','AG','AH','AI','AJ','AK','AL','AM','AN','AO','AP','AQ','AR','AS','AT','AU','AV','AW','AX','AY','AZ');
			
			$objPHPExcel->getActiveSheet(0)->mergeCells('A1:'.$cellName[$cellNum-1].'1');//合并单元格
		   // $objPHPExcel->setActiveSheetIndex(0)->setCellValue('A1', $expTitle.'  Export time:'.date('Y-m-d H:i:s'));
			for($i=0;$i<$cellNum;$i++){
				$objPHPExcel->setActiveSheetIndex(0)->setCellValue($cellName[$i].'2', $expCellName[$i][1]); 
			}
			// Miscellaneous glyphs, UTF-8   
			for($i=0;$i<$dataNum;$i++){
			  for($j=0;$j<$cellNum;$j++){
				$objPHPExcel->getActiveSheet(0)->setCellValue($cellName[$j].($i+3), $expTableData[$i][$expCellName[$j][0]]);
			  }             
			}
			
			header('pragma:public');
			header('Content-type:application/vnd.ms-excel;charset=utf-8;name="'.$xlsTitle.'.xls"');
			header("Content-Disposition:attachment;filename=$fileName.xls");//attachment新窗口打印inline本窗口打印
			$objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');  
			$objWriter->save('php://output');
			exit;
	}
	//编辑商品
	public function goods_edit(){
		$datas=file_get_contents('php://input');
		file_put_contents('./log/goods_edit.log',date('Y-m-d H:i:s').var_export($datas,true).PHP_EOL,FILE_APPEND);//日志导出sql
		$param=json_decode($datas,true);
		// dump($param);
		$warehouse_code		=$param['warehouse_code'];
		$map['id']=$param['id'];
		if(!empty($param['cloud_depot_status'])){
			$data['cloud_depot_status']=$param['cloud_depot_status'];
		}
		if(!empty($param['cloud_num'])){
			$data['cloud_num']=$param['cloud_num'];
		}
		if(!empty($param['cloud_unit'])){
			$data['cloud_unit']=$param['cloud_unit'];
		}
		// if(!empty($param['freez_amount'])){
			// $data['freez_amount']=$param['freez_amount'];
		// }
		// if(!empty($param['storage_amount'])){
			// $data['storage_amount']=$param['storage_amount'];
		// }
		
		// dump($data);
		$this->db_config($warehouse_code);
		$m=M('ArrivalOrder',C('DB_PRS'),C('DB_DEPOT'));
		$info=$m->where($map)->find();
		if($info['storage_amount']<$param['cloud_num']){
			$return['status']=0;
			$return['info']="不能大于库存数量！";
			$this->ajaxReturn($return);
		}
		$res=$m->where($map)->save($data);
		$sql=$m->getlastsql();
		file_put_contents('./log/goods_edit.log',date('Y-m-d H:i:s').var_export($sql,true).PHP_EOL,FILE_APPEND);
		if($res!==false){
			$return['status']=1;
			$return['info']="修改成功！";
			$this->ajaxReturn($return);
		}else{
			$return['status']=0;
			$return['info']="修改失败！";
			$this->ajaxReturn($return);
		}
	}
	//实时库存
    public function goods(){
		$m=M('Warehouse');
		//验证用户名/密码--begin
		// $username=I('username');
		// $password=I('password');
		// if(){
			
		// }
		// md5($username.$password)
		//验证用户名/密码--end
		$Model = new Model();
		$count=0;

		$datas=file_get_contents('php://input');
		$param=json_decode($datas,true);
		file_put_contents('./log/goods.log',date('Y-m-d H:i:s').var_export($datas,true).PHP_EOL,FILE_APPEND);//日志导出sql
		
		$erp_code=$param['erp_code'];
		$warehouse_code=$param['warehouse_code'];//仓库编码
		
		if(empty($warehouse_code) && empty($erp_code)){
			$return['status']=0;
			$return['info']="请传入仓库编码或企业编码！";
			$this->ajaxReturn($return);
		}
				
		if(empty($erp_code)){
			$uw=M('UserWarehouse');
			$uw_info=$uw->field('warehouse_group_id')->where(array('user_id'=>$erp_code))->find();
			$map_w['id']=array('in',$uw_info['warehouse_group_id']);
			$warehouse_list=$m->where($map_w)->select();
		}
		if(empty($warehouse_code)){
			$map['status']=1;
			$map['warehouse_code']=array('in',$warehouse_code);
			$warehouse_list=$m->where($map)->select();
		}
		
		
		$goods_batch=$param['goods_batch'];
		if(empty($goods_batch)){
			$goods_batch=0;
		}
		// echo $m->getlastsql();
		$where="where 1 ";
		
		//时间查询-begin
		$start_time=$param['start_time'];//开始时间
		$end_time=$param['end_time'];//结束时间
		if(!empty($start_time)) {
			$start_time=strtotime($start_time);
			$where.="and add_time > '$start_time' ";
        }
        if(!empty($end_time)) {
			$end_time=strtotime($end_time);
			$where.="and add_time < '$end_time' ";
        }
		if(!empty($start_time) && !empty($end_time)) {
			$start_time=strtotime($start_time);
			$end_time=strtotime($end_time);
            $where['add_time']= array(array('gt',I("start_time")),array('lt',I("end_time")));
			$where.="and (add_time > '$start_time' and add_time < '$end_time') ";
        }
		//时间查询-end
		$erp_code=$param['erp_code'];//企业编码
		if(!empty($erp_code)){
			$where.="and erp_code like '%$erp_code%' ";
		}
		
		$goods_no=$param['goods_no'];//商品编码
		if(!empty($goods_no)){
			$where.="and goods_no like '%$goods_no%' ";
		}
		
		$goods_name=$param['goods_name'];//商品名称
		if(!empty($goods_name)){
			$where.="and goods_name like '%$goods_name%' ";
		}
		
		$listrows=$param['listrows'];//每页显示条数
		if(!empty($listrows)){
			$listrows=$listrows;
		}else{
			$listrows=20;
		}
		
		$p=$param['p'];//页数
		if(!empty($p)){
			$p=$p;
		}else{
			$p=1;
		}
		$limit="limit ".($p-1)*$listrows.",".$listrows;

		foreach($warehouse_list as $k=>$v){
			$this->db_config($v['warehouse_code']);
			$ao=M('ArrivalOrder',C('DB_PRS'),C('DB_DEPOT'));
			$count_s=$ao->count();
			$count=$count+$count_s;//总条数
			if($k==0){
				if($goods_batch){//库存汇总数据
					$sql="select * from (select *,sum('receive_amount') a_receive_amount,sum('freez_amount') a_freez_amount,sum('storage_amount') a_storage_amount from ".C('DB_DEPOT.DB_NAME').".".C('DB_PRS')."arrival_order ".$where."group by goods_no,manual_id ";
				}else{//批次库存数据
					$sql="select * from (select *,".$v['warehouse_code']." warehouse_code from ".C('DB_DEPOT.DB_NAME').".".C('DB_PRS')."arrival_order ".$where;
				}
			}else{
				if($goods_batch){
					$sql.=" union all select *,sum('receive_amount') a_receive_amount,sum('freez_amount') a_freez_amount,sum('storage_amount') a_storage_amount from ".C('DB_DEPOT.DB_NAME').".".C('DB_PRS')."arrival_order ".$where."group by goods_no,manual_id ";
				}else{
					$sql.=" union all select *,".$v['warehouse_code']." warehouse_code from ".C('DB_DEPOT.DB_NAME').".".C('DB_PRS')."arrival_order ".$where;
				}
			}
		}
		$sql.=") t ".$limit;
		// echo $sql;
		// die;
		file_put_contents('./log/goods.log',date('Y-m-d H:i:s').var_export($sql,true).PHP_EOL,FILE_APPEND);//日志导出sql
		$list=$Model->query($sql);
		if(empty($list)){
			$return['status']=0;
			$return['info']="没有数据！";
			$this->ajaxReturn($return);
		}
		$return['status']=1;
		$return['info']['count']=$count;
		$return['info']['list']=$list;
		$this->ajaxReturn($return);
	}

	//云仓库存数量
	public function yuncang_storage(){
		$m=M('Warehouse');
		
		$Model = new Model();
		$count=0;

		$datas=file_get_contents('php://input');
		
		$param=json_decode($datas,true);
		file_put_contents('./log/yuncang_storage.log',date('Y-m-d H:i:s').var_export($datas,true).PHP_EOL,FILE_APPEND);//日志导出sql
		
		$erp_code=$param['erp_code'];
		$warehouse_code=$param['warehouse_code'];//仓库编码
		
		if(empty($warehouse_code) && empty($erp_code)){
			$return['status']=0;
			$return['info']="请传入仓库编码或企业编码！";
			$this->ajaxReturn($return);
		}
		
		if(!empty($warehouse_code) && !empty($erp_code)){
			$return['status']=0;
			$return['info']="一次只能传一个企业编码或者仓库编码！";
			$this->ajaxReturn($return);
		}
				
		if(!empty($erp_code)){
			$uw=M('UserWarehouse');
			$uw_info=$uw->field('warehouse_group_id')->where(array('user_id'=>$erp_code))->find();
			$map_w['id']=array('in',$uw_info['warehouse_group_id']);
			$warehouse_list=$m->where($map_w)->select();
		}
		
		if(!empty($warehouse_code)){
			$map['status']=1;
			$map['warehouse_code']=array('in',$warehouse_code);
			$warehouse_list=$m->where($map)->select();
		}
		
		$goods_batch=$param['goods_batch'];
		if(empty($goods_batch)){
			$goods_batch=0;
		}
		// echo $m->getlastsql();
		$wheres="where ";
		$where=" 1 ";
		
		//时间查询-begin
		$start_time=$param['start_time'];//开始时间
		$end_time=$param['end_time'];//结束时间
		if(!empty($start_time)) {
			$start_time=strtotime($start_time);
			$where.="and add_time > '$start_time' ";
        }
        if(!empty($end_time)) {
			$end_time=strtotime($end_time);
			$where.="and add_time < '$end_time' ";
        }
		if(!empty($start_time) && !empty($end_time)) {
			$start_time=strtotime($start_time);
			$end_time=strtotime($end_time);
            // $where['add_time']= array(array('gt',I("start_time")),array('lt',I("end_time")));
			$where.="and (add_time > '$start_time' and add_time < '$end_time') ";
        }
		//时间查询-end
		$erp_code=$param['erp_code'];//企业编码
		if(!empty($erp_code)){
			$where.="and erp_code like '%$erp_code%' ";
		}
		
		$goods_no=$param['goods_no'];//商品编码
		if(!empty($goods_no)){
			$where.="and goods_no like '%$goods_no%' ";
		}
		
		$goods_name=$param['goods_name'];//商品名称
		if(!empty($goods_name)){
			$where.="and goods_name like '%$goods_name%' ";
		}
		
		$listrows=$param['listrows'];//每页显示条数
		if(!empty($listrows)){
			$listrows=$listrows;
		}else{
			$listrows=20;
		}
		
		$p=$param['p'];//页数
		if(!empty($p)){
			$p=$p;
		}else{
			$p=1;
		}
		$limit="limit ".($p-1)*$listrows.",".$listrows;

		foreach($warehouse_list as $k=>$v){
			$this->db_config($v['warehouse_code']);
			$ao=M('ArrivalOrder',C('DB_PRS'),C('DB_DEPOT'));
			$count_s=$ao->where($where)->count();
			$count=$count+$count_s;//总条数
			if($k==0){
					$sql="select * from (select gross_weight,net_weight,goods_no,goods_name,source_no,unit1,price,sum('cloud_storage_num') storage_num from ".C('DB_DEPOT.DB_NAME').".".C('DB_PRS')."arrival_order ".$wheres.$where."group by goods_no,manual_id ";
			}else{
					$sql.=" union all select gross_weight,net_weight,goods_no,goods_name,source_no,unit1,price,sum('cloud_storage_num') storage_num from ".C('DB_DEPOT.DB_NAME').".".C('DB_PRS')."arrival_order ".$wheres.$where."group by goods_no,manual_id ";
			}
		}
		$sql.=") t ".$limit;
		// echo $sql;
		// die;
		file_put_contents('./log/yuncang_storage.log',date('Y-m-d H:i:s').var_export($sql,true).PHP_EOL,FILE_APPEND);//日志导出sql
		$list=$Model->query($sql);
		if(empty($list)){
			$return['status']=0;
			$return['info']="没有数据！";
			$this->ajaxReturn($return);
		}else{
			foreach($list as $ks=>$vs){
				$unit=M('Unit',C('DB_PRS'),C('DB_DEPOT'));
				$unit_info=$unit->where(array('unit_no'=>$vs['unit1']))->find();
				$list[$ks]['unit1_name']=$unit_info['unit_name'];
			}
		}
		$return['status']=1;
		$return['info']['count']=$count;
		$return['info']['list']=$list;
		$this->ajaxReturn($return);
	}
	//订单统计
	public function order(){
		$w=M('Warehouse');
		$Model = new Model();
		$datas=file_get_contents('php://input');
		
		$param=json_decode($datas,true);
		
		$warehouse_code=$param['warehouse_code'];
		$erp_code=$param['erp_code'];
		
		if(empty($warehouse_code)){
			$return['status']=0;
			$return['info']="请传入仓库编码1！";
			$this->ajaxReturn($return);
		}
		
		if(!empty($warehouse_code)){
			$where['status']=1;
			$where['warehouse_code']=array('in',$warehouse_code);
			$warehouse_list=$w->where($where)->select();
		}
		
		// dump($warehouse_list);
		$start_time=$param['start_time'];
		$end_time=$param['end_time'];
		
		$goods_no=$param['goods_no'];
		$goods_name=$param['goods_name'];
		$come_from=$param['come_from'];
		$merchant_code=$param['merchant_code'];
		
		if(!empty($goods_no)){
			$map['goods_no']=array('like',"%$goods_no%");
		}
		if(!empty($goods_name)){
			$map['goods_name']=array('like',"%$goods_name%");
		}
		if(!empty($come_from)){
			$map['come_from']=$come_from;
		}

		$map['add_time']= array(array('gt',$start_time),array('lt',$end_time));

		$nums_month=getMonthNum(date('Y-m-d',$start_time),date('Y-m-d',$end_time));
		
		foreach($warehouse_list as $k=>$v){
			$this->db_config($v['warehouse_code']);
			
			$m=M('Pushorder',C('DB_PRS'),C('DB_DEPOT'));
			if($end_time-$start_time<=86400){//一天内
				//按小时统计
				if(!empty($goods_no) or !empty($goods_name)){
					$list_sql=$m->alias('m')->field("count(1) t_count,FROM_UNIXTIME(add_time,'%Y-%m-%d %H:00:00') t_time")->join('__PUSHORDERGOODS__ g on m.id=g.order_id','right')->where($map)->group("FROM_UNIXTIME(add_time,'%Y-%m-%d %H')")->select(false);//时间列表
				}else{
					$list_sql=$m->field("count(1) t_count,FROM_UNIXTIME(add_time,'%Y-%m-%d %H:00:00') t_time")->where($map)->group("FROM_UNIXTIME(add_time,'%Y-%m-%d %H')")->select(false);//时间列表
				}
			}
			
			if($end_time-$start_time>86400 and $nums_month<=1){//一个月内
				//按天统计
				if(!empty($goods_no) or !empty($goods_name)){
					$list_sql=$m->alias('m')->field("count(1) t_count,FROM_UNIXTIME(add_time,'%Y-%m-%d') t_time")->join('__PUSHORDERGOODS__ g on m.id=g.order_id','right')->where($map)->group("FROM_UNIXTIME(add_time,'%Y-%m-%d')")->select(false);//时间列表
				}else{
					$list_sql=$m->field("count(1) t_count,FROM_UNIXTIME(add_time,'%Y-%m-%d') t_time")->where($map)->group("FROM_UNIXTIME(add_time,'%Y-%m-%d')")->select(false);//时间列表
				}
				
			}
			
			if($nums_month>1 && $nums_month<=12){//一年内
				//按月统计
				if(!empty($goods_no) or !empty($goods_name)){
					$list_sql=$m->alias('m')->field("count(1) t_count,FROM_UNIXTIME(add_time,'%Y-%c') t_time")->join('__PUSHORDERGOODS__ g on m.id=g.order_id','right')->where($map)->group("FROM_UNIXTIME(add_time,'%Y-%m')")->select(false);//时间列表
				}else{
					$list_sql=$m->field("count(1) t_count,FROM_UNIXTIME(add_time,'%Y-%c') t_time")->where($map)->group("FROM_UNIXTIME(add_time,'%Y-%m')")->select(false);//时间列表
				}
			}
			
			file_put_contents('./log/order.log',date('Y-m-d H:i:s').var_export($m->getlastsql(),true).PHP_EOL,FILE_APPEND);//日志导出sql
			
			$list_goods_sql=$m->alias('m')->field("count(1) t_count,g.goods_no,g.goods_name")->join('__PUSHORDERGOODS__ g on m.id=g.order_id','right')->where($map)->group("m.logistics_business")->order('t_count desc')->limit(10)->select(false);//商品列表
		
			if(!empty($goods_no) or !empty($goods_name)){
				$list_company_sql=$m->alias('m')->field("count(1) t_count,company_code,company_name")->join('__PUSHORDERGOODS__ g on m.id=g.order_id','right')->where($map)->group("company_code")->order('t_count desc')->limit(10)->select(false);//电商企业名称列表
			}else{
				$list_company_sql=$m->field("count(1) t_count,company_code,company_name")->where($map)->group("company_code")->order('t_count desc')->limit(10)->select(false);//电商企业名称列表
			}
			
			if(!empty($goods_no) or !empty($goods_name)){
				$list_logistics_business_sql=$m->alias('m')->field("count(1) t_count,logistics_business")->join('__PUSHORDERGOODS__ g on m.id=g.order_id','right')->where($map)->group("logistics_business")->order('t_count desc')->limit(10)->select(false);//物流商家列表
			}else{
				$list_logistics_business_sql=$m->field("count(1) t_count,logistics_business")->where($map)->group("logistics_business")->order('t_count desc')->limit(10)->select(false);//物流商家列表
			}
			
			if(!empty($goods_no) or !empty($goods_name)){
				$list_comefrom_sql=$m->alias('m')->field("count(1) t_count,come_from")->join('__PUSHORDERGOODS__ g on m.id=g.order_id','right')->where($map)->group("come_from")->order('t_count desc')->limit(10)->select(false);//平台列表
			}else{
				$list_comefrom_sql=$m->field("count(1) t_count,come_from")->where($map)->group("come_from")->order('t_count desc')->limit(10)->select(false);//平台列表
			}
		
			if($k==0){
				$list_sql_l=$list_sql;
				$list_goods_sql_l=$list_goods_sql;
				$list_company_sql_l=$list_company_sql;
				$list_logistics_business_sql_l=$list_logistics_business_sql;
				$list_comefrom_sql_l=$list_comefrom_sql;
			}else{
				$list_sql_l.=" union all".$list_sql;
				$list_goods_sql_l=" union all".$list_goods_sql;
				$list_company_sql_l=" union all".$list_company_sql;
				$list_logistics_business_sql_l=" union all".$list_logistics_business_sql;
				$list_comefrom_sql_l=" union all".$list_comefrom_sql;
			}
		}
		$list_sql_l="select sum(t_count) t_count,t_time from (".$list_sql_l.") t group by t_time";
		$list=$Model->query($list_sql_l);
		
		$list_goods_sql_l="select sum(t_count) t_count,goods_no,goods_name from (".$list_goods_sql_l.") t group by goods_no limit 0,10";
		$list_goods=$Model->query($list_goods_sql_l);
		
		$list_company_sql_l="select sum(t_count) t_count,company_code,company_name from (".$list_company_sql_l.") t group by company_code limit 0,10";
		$list_company=$Model->query($list_company_sql_l);
		
		$list_logistics_business_sql_l="select sum(t_count) t_count,logistics_business from (".$list_logistics_business_sql_l.") t group by logistics_business limit 0,10";
		$list_logistics_business=$Model->query($list_logistics_business_sql_l);
		
		$list_comefrom_sql_l="select sum(t_count) t_count,come_from from (".$list_comefrom_sql_l.") t group by come_from limit 0,10";
		$list_comefrom=$Model->query($list_comefrom_sql_l);
		
		
		// echo $list_sql_l;
		// die;
		$count_all=0;
			
		if($end_time-$start_time<=86400){//一天内
			$categories="";
			$datas="";
			$categories=',"00:00","01:00","02:00","03:00","04:00","05:00","06:00","07:00","08:00","09:00","10:00","11:00","12:00","13:00","14:00","15:00","16:00","17:00","18:00","19:00","20:00","21:00","22:00","23:00","24:00"';
			$cate_list=explode(',',substr($categories,1));
			foreach($cate_list as $ks=>$vs){
				$ii=0;
				foreach($list as $k=>$v){
					$ii++;
					if($ks==intval(date("H",strtotime($v['t_time'])))){
						$datas.=','.$v['t_count'];
						$count_all+=$v['t_count'];
						break;
					}
					if($ii==count($list)){
						$datas.=',0';
					}
				}
			}
		}

		if($end_time-$start_time>86400 and $nums_month<=1){//一个月内
			$categories="";
			$datas="";
			$days=($end_time-$start_time)/86400+1;//相差天数
			for($i=0;$i<$days;$i++){
				$categories.=',"'.date('Y-m-d',$start_time).'"';
				$ii=0;
				foreach($list as $k=>$v){
					$ii++;
					if(date('Y-m-d',$start_time)==$v['t_time']){
						$datas.=','.$v['t_count'];
						$count_all+=$v['t_count'];
						break;
					}
					if($ii==count($list)){
						$datas.=',0';
					}
				}
				$start_time+=86400;
			}
		}
		
		if($nums_month>1 && $nums_month<=12){//一年内
			$categories="";
			$datas="";
			$t_month=intval(date('m',$start_time));//开始月份
			$t_year=intval(date('Y',$start_time));//开始月份
			$datename=date('Y-'.$t_month,$start_time);//开始日期
			$next_month=0;//下一年开始月份
			for($i=0;$i<$nums_month+1;$i++){
				$categories.=',"'.$datename.'"';
				$ii=0;
				foreach($list as $k=>$v){
					$ii++;
					if($datename==$v['t_time']){
						$datas.=','.$v['t_count'];
						$count_all+=$v['t_count'];
						break;
					}
					if($ii==count($list)){//如果找不到匹配时间（即查询结果中无结果，则返回值为0）
						$datas.=',0';
					}
				}
				if($t_month+$i<12){
					$datename=date('Y-'.($t_month+$i+1),$start_time);//更新下月时间
					
				}else{
					$next_month++;
					$datename=($t_year+1).'-'.$next_month;//更新下月时间
				}
			}
		}
		
		if($nums_month>12){//大于1年
			$this->error('查询时间不能大于一年！');
		}
		
		// dump($list_goods);
		// dump($categories);
		// dump($datas);
		// dump($datas);
		// dump($categories);
		// echo $m->getlastsql();
		
		
		
		$return['status']=1;
		$return['info']['list_company']=$list_company;
		$return['info']['list_goods']=$list_goods;
		$return['info']['list_logistics_business']=$list_logistics_business;
		$return['info']['list_comefrom']=$list_comefrom;
		$return['info']['count_all']=$count_all;
		$return['info']['categories']=substr($categories,1);
		$return['info']['datas']=substr($datas,1);
		$return['info']['list_t']=$list_t;
		
		
		$this->ajaxReturn($return);
    }
	//盘库
	public function checkstock(){
		$datas=file_get_contents('php://input');
		$param=json_decode($datas,true);
		$warehouse_code=$param['warehouse_code'];
		$order_no=$param['order_no'];
		$manua_id=$param['manua_id'];
		$erp_code=$param['erp_code'];					   
		
		$this->db_config($warehouse_code);
		$m=M('Checkstock',C('DB_PRS'),C('DB_DEPOT'));
		$n=M('CheckstockInfo',C('DB_PRS'),C('DB_DEPOT'));
		$g=M('Goods',C('DB_PRS'),C('DB_DEPOT'));
		
		file_put_contents('./log/checkstock.log',date('Y-m-d H:i:s').var_export($param,true).PHP_EOL,FILE_APPEND);//日志导出sql
		
		if(empty($warehouse_code)){
			$return['status']=0;
			$return['info']="请传入仓库编码！";
			$this->ajaxReturn($return);
		}
		
		if(empty($order_no)){
			$return['status']=0;
			$return['info']="请传入盘库单号！";
			$this->ajaxReturn($return);
		}
		
		if(empty($manua_id)){
			$return['status']=0;
			$return['info']="请传入账册编号！";
			$this->ajaxReturn($return);
		}
		unset($param['warehouse_code']);
		$dup=$m->where(array('old_order_no'=>$order_no))->find();
		if($dup){
			$return['status']=0;
			$return['info']="单号已经存在！";
			$this->ajaxReturn($return);
		}
		$data_add['manua_id']=$manua_id;
		$data_add['old_order_no']=$order_no;
		$data_add['order_no']=$manua_id.$order_no;
		$data_add['add_time']=time();
		$data_add['update_time']=time();
		$data_add['section_no']='erp';
		$data_add['people_make_no']='erp';
		$data_add['people_purchasing_no']='erp';
		$data_add['erp_code']=$erp_code;						  

		$res=$m->add($data_add);
		if($res){
			$list_goods=$g->alias('m')->field('m.*,sum(a.freez_amount) freez_amount,sum(a.storage_amount) storage_amount2')->join("__ARRIVAL_ORDER__ a on m.goods_no=a.goods_no and m.manua_id=a.manual_id","left")->group('m.goods_no,m.manua_id')->where(array('m.manua_id'=>$manua_id))->select();

			if(count($list_goods)==0){
				$return['info']='此账册下没有查到商品库存信息，不能添加！';
				$return['status']=0;
				$this->ajaxReturn($return);
			}
			//添加盘库详情信息
			foreach($list_goods as $k=>$v){
				if(empty($v['storage_amount2'])){
					$v['storage_amount2']=0;
				}
				if(empty($v['conforming_p'])){
					$v['conforming_p']=0;
				}
				if(empty($v['nonconforming_p'])){
					$v['nonconforming_p']=0;
				}
				if(empty($v['scrap_one'])){
					$v['scrap_one']=0;
				}
				$datas_info['manua_id']=$v['manua_id'];
				$datas_info['source_no']=$v['source_no'];
				$datas_info['goods_name']=$v['goods_name'];
				$datas_info['goods_no']=$v['goods_no'];
				$datas_info['goods_spec']=$v['goods_spec'];
				$datas_info['amount']=$v['storage_amount2'];
				$datas_info['bar_code']=$v['bar_code'];
				$datas_info['conforming_p']=$v['conforming_p'];
				$datas_info['nonconforming_p']=$v['nonconforming_p'];
				$datas_info['scrap_one']=$v['scrap_one'];
				$datas_info['order_id']=$res;
				$status=$n->add($datas_info);
				// echo $n->getDbError();
				// $return['info']=$datas;
					// $return['status']=0; 
					// $this->ajaxReturn($return);
				if(!$status){
					$return['info']='添加盘库详情失败!';
					$return['status']=0; 
					$this->ajaxReturn($return);
				}
			}
			$return['status']=1;
			$return['info']="添加成功！";
			$this->ajaxReturn($return);
		}else{
			$return['status']=0;
			$return['info']="添加失败！";
			$this->ajaxReturn($return);
		}
	}
	//下盘库单erp
    public function do_checkstock(){
        $datas=file_get_contents('php://input');
        $param=json_decode($datas,true);
        $warehouse_code=$param['warehouse_code'];
        $order_no=$param['order_no'];
        $manua_id=$param['manua_id'];
        $erp_code=$param['erp_code'];
        $goods_list = $param['goods_list'];

        $this->db_config($warehouse_code);
        $m=M('Checkstock',C('DB_PRS'),C('DB_DEPOT'));
        $n=M('CheckstockInfo',C('DB_PRS'),C('DB_DEPOT'));
        $g=M('Goods',C('DB_PRS'),C('DB_DEPOT'));

        //file_put_contents('./log/checkstock_erp.log',date('Y-m-d H:i:s').var_export($param,true).PHP_EOL,FILE_APPEND);//日志导出sql

        if(empty($warehouse_code)){
            $return['status']=0;
            $return['info']="请传入仓库编码！";
            $this->ajaxReturn($return);
        }

        if(empty($order_no)){
            $return['status']=0;
            $return['info']="请传入盘库单号！";
            $this->ajaxReturn($return);
        }

        if(empty($manua_id)){
            $return['status']=0;
            $return['info']="请传入账册编号！";
            $this->ajaxReturn($return);
        }

        //可对单个商品盘库
        $goods_ids = array();
        foreach($goods_list as $val){
            $goods_ids[]= $val['goods_no'];
        }
        if(empty($goods_ids)){
            $return['status']=0;
            $return['info']="请传入商品信息！";
            $this->ajaxReturn($return);
        }

        unset($param['warehouse_code']);
        $dup=$m->where(array('old_order_no'=>$order_no))->find();
        if($dup){
            $return['status']=0;
            $return['info']="单号已经存在！";
            $this->ajaxReturn($return);
        }
        $data_add['manua_id']=$manua_id;
        $data_add['old_order_no']=$order_no;
        $data_add['order_no']=$manua_id.$order_no;
        $data_add['add_time']=time();
        $data_add['update_time']=time();
        $data_add['section_no']='erp';
        $data_add['people_make_no']='erp';
        $data_add['people_purchasing_no']='erp';
        $data_add['erp_code']=$erp_code;


        $res=$m->add($data_add);
        if($res){
            $list_goods=$g->alias('m')->field('m.*,sum(a.freez_amount) freez_amount,sum(a.storage_amount) storage_amount2')->join("__ARRIVAL_ORDER__ a on m.goods_no=a.goods_no and m.manua_id=a.manual_id","left")->group('m.goods_no,m.manua_id')->where(array('m.manua_id'=>$manua_id,'m.goods_no'=>array('in',$goods_ids)))->select();

            if(count($list_goods)==0){
                $return['info']='此账册下没有查到商品库存信息，不能添加！';
                $return['status']=0;
                $this->ajaxReturn($return);
            }
            //添加盘库详情信息
            foreach($list_goods as $k=>$v){
                if(empty($v['storage_amount2'])){
                    $v['storage_amount2']=0;
                }
                if(empty($v['conforming_p'])){
                    $v['conforming_p']=0;
                }
                if(empty($v['nonconforming_p'])){
                    $v['nonconforming_p']=0;
                }
                if(empty($v['scrap_one'])){
                    $v['scrap_one']=0;
                }
                $datas_info['manua_id']=$v['manua_id'];
                $datas_info['source_no']=$v['source_no'];
                $datas_info['goods_name']=$v['goods_name'];
                $datas_info['goods_no']=$v['goods_no'];
                $datas_info['goods_spec']=$v['goods_spec'];
                $datas_info['amount']=$v['storage_amount2'];
                $datas_info['bar_code']=$v['bar_code'];
                $datas_info['conforming_p']=$v['conforming_p'];
                $datas_info['nonconforming_p']=$v['nonconforming_p'];
                $datas_info['scrap_one']=$v['scrap_one'];
                $datas_info['order_id']=$res;
                $status=$n->add($datas_info);
                // echo $n->getDbError();
                // $return['info']=$datas;
                // $return['status']=0;
                // $this->ajaxReturn($return);
                if(!$status){
                    $return['info']='添加盘库详情失败!';
                    $return['status']=0;
                    $this->ajaxReturn($return);
                }
            }
            $return['status']=1;
            $return['info']="添加成功！";
            $this->ajaxReturn($return);
        }else{
            $return['status']=0;
            $return['info']="添加失败！";
            $this->ajaxReturn($return);
        }
    }
	/*
	 *获取数据库配置
     */
    public function db_config($warehouse_code){
		
		$info=M('Warehouse')->where(array('warehouse_code'=>$warehouse_code))->find();//获取配置表中对应域名的id
		$ss=M('Warehouse')->getlastsql();
		file_put_contents('./log/db_config.log',date('Y-m-d H:i:s').var_export($ss,true).PHP_EOL,FILE_APPEND);//日志导出sql
		// echo M()->getlastsql();
		
		// dump($info);
		if($info){  //天众数据库库配置
			!empty($info['db_ip'])?$db_ip=$info['db_ip']:$db_ip=C('DEPOT_DB_HOST');
			!empty($info['db_name'])?$db_name=$info['db_name']:$db_name=C('DEPOT_DB_NAME');
			!empty($info['db_username'])?$db_username=$info['db_username']:$db_username=C('DEPOT_DB_USER');
			!empty($info['db_password'])?$db_password=$info['db_password']:$db_password=C('DEPOT_DB_PWD');
			!empty($info['db_port'])?$db_port=$info['db_port']:$db_port=C('DEPOT_DB_PORT');
			!empty($info['db_pre'])?$db_pre=$info['db_pre']:$db_pre=C('DEPOT_DB_PREFIX');
			
			//当前仓库数据库配置
			C('DB_DEPOT.DB_TYPE','mysql');
			C('DB_DEPOT.DB_HOST',$db_ip);
			C('DB_DEPOT.DB_NAME',$db_name);
			C('DB_DEPOT.DB_USER',$db_username);
			C('DB_DEPOT.DB_PWD',$db_password);
			C('DB_DEPOT.DB_PORT',$db_port);
			C('DB_PRS',$db_pre);
			C('DB_DEPOT.DB_CHARSET','utf8');
		}
		
		//加载自定义配置信息
		$m=M('Config',C('DB_PRS'),C('DB_DEPOT'));
		$list=$m->where(array('status'=>1))->getField('en_name,value');
		C('base_config',$list);
    }
}